using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// Book 的摘要说明。
	/// </summary>
	public class Book : BaseDAL<BookInfo>, IBook
	{
		#region 对象实例及构造函数

		public static Book Instance
		{
			get
			{
				return new Book();
			}
		}
		public Book() : base("KF_Book","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override BookInfo DataReaderToEntity(IDataReader dataReader)
		{
			BookInfo bookInfo = new BookInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			bookInfo.ID = reader.GetInt32("ID");
            bookInfo.RoomNo = reader.GetString("RoomNo");
			bookInfo.BookNo = reader.GetString("BookNo");
			bookInfo.CustomerName = reader.GetString("CustomerName");
			bookInfo.CustomerTel = reader.GetString("CustomerTel");
			bookInfo.CustomerCompany = reader.GetString("CustomerCompany");
			bookInfo.Source = reader.GetString("Source");
			bookInfo.PreArrivalTime = reader.GetDateTime("PreArrivalTime");
			bookInfo.PreOffTime = reader.GetDateTime("PreOffTime");
			bookInfo.RetentionTime = reader.GetDateTime("RetentionTime");
			bookInfo.Note = reader.GetString("Note");
            bookInfo.BookStatus = reader.GetInt32("IsCancel");
            bookInfo.IsFixRoomNo = reader.GetBoolean("IsFixRoomNo");
            bookInfo.RoomCount = reader.GetInt32("RoomCount");
            bookInfo.RoomType = reader.GetString("RoomType");
            bookInfo.BookPeople = reader.GetString("BookPeople");
            bookInfo.BookPeoplePhone = reader.GetString("BookPeoplePhone");
            bookInfo.IsTeam = reader.GetInt32("IsTeam");
            bookInfo.Account = reader.GetString("Account");
			
			return bookInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(BookInfo obj)
		{
		    BookInfo info = obj as BookInfo;
			Hashtable hash = new Hashtable(); 
			
            hash.Add("RoomNo", info.RoomNo);
 			hash.Add("BookNo", info.BookNo);
 			hash.Add("CustomerName", info.CustomerName);
 			hash.Add("CustomerTel", info.CustomerTel);
 			hash.Add("CustomerCompany", info.CustomerCompany);
 			hash.Add("Source", info.Source);
 			hash.Add("PreArrivalTime", info.PreArrivalTime);
 			hash.Add("PreOffTime", info.PreOffTime);
 			hash.Add("RetentionTime", info.RetentionTime);
 			hash.Add("Note", info.Note);
            hash.Add("IsCancel", info.BookStatus);
            hash.Add("IsFixRoomNo", info.IsFixRoomNo);
            hash.Add("RoomType", info.RoomType);
            hash.Add("RoomCount", info.RoomCount);
            hash.Add("BookPeople", info.BookPeople);
            hash.Add("BookPeoplePhone", info.BookPeoplePhone);
            hash.Add("IsTeam", info.IsTeam);
            hash.Add("Account", info.Account);
 				
			return hash;
		}

        public bool UpdateBookIn(int id, int status)
        {
            string sql = string.Format("update {0} set IsCancel={1} where ID ={2}",
                tableName, status, id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        }


        public bool UpdateTeamBook(string bookNum, int status)
        {

            string sql = string.Format("update {0} set IsCancel={1} where BookNo ='{2}'",
                          tableName, status, bookNum);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        }

        public bool DelTeamBook(string bookNum)
        {


            string sql = string.Format("delete from  {0} where  BookNo ='{1}'",
                          tableName, bookNum);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        
        }
    
    }
}